package com.lyy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lyy.fallback.SentinelFallback;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Result;
import java.util.concurrent.TimeUnit;

/**
 * @author Liangyaoyun
 */
@RestController
@RequestMapping("/api/v1/degrade")
public class DegradeController {
    /** 当资源 `rt` 降级时，执行SentinelFallback类的fallback()方法 */
    @SentinelResource(value = "rt",
            fallbackClass = SentinelFallback.class,
            fallback = "fallback")
    @RequestMapping("/rt")
    @SneakyThrows
    public String rt() {
        TimeUnit.SECONDS.sleep(1L);
        return "资源rt调用成功";
    }

    private int exRationNum=0;

//    public Result exRation(){
//        // 配合计数器属性，每访问到第3次就抛出一个异常，以模拟0.33的异常率
//        if (++exRationNum%3==0){
//            throw new RuntimeException("资源exRation人为异常");
//        }
//
//    }
}
